import math
import tkinter as tk#导入
from tkinter import *
import re


import pandas as pd
class NumToUpper:
    def __init__(self):
        self.number='0'
        self.Uppercase='零元整'
        self.NU=pd.Series(['零','壹','贰','叁','肆','伍','陆','柒','捌','玖'],index=[0,1,2,3,4,5,6,7,8,9])#数字对应
        self.ND=pd.Series([['元','万','亿','兆'],'拾','佰','仟','角','分'],index=[1,2,3,0,-1,-2]) #单位对应
    def addNumber(self,num):
        if self.number =='0':
            if num=='0' or num=='00':
                print("请正确输入数字")
                return 0
            else:
                self.number=num
                return 1
        elif len(self.number) > 3 and self.number[-3] == '.':
            print("小数点后个数限两位")
            return 0
        else:
            self.number+=num
            return  1
    def setNumber(self,num):
        self.number=num
    def removeNumber(self):
        self.number=self.number[:-1]
    def clear(self):
        self.number='0'
        self.Uppercase='零元整'
    def displayNumber(self):
        return self.number

    def get_decimal_part_length(self):
        parts = self.number.split('.')
        if len(parts) > 1:
            return len(parts[0])
        else:
            return len(self.number)

    def displayUpper(self):
        self.Uppercase = ''#以2232.34为例
        intlength = self.get_decimal_part_length()#求长度
        length=len(self.number)
        for i in range(intlength):
            self.Uppercase+=self.NU[eval(self.number[i])]#第一个数总是中文汉字
            zheng=math.floor((intlength-i)/4)
            yu=math.floor((intlength-i)%4)
            if yu==1:#"元万亿兆"中选择
                if self.Uppercase[-1] == '零' and len!=1:
                    self.Uppercase = self.Uppercase[0:-1] + self.ND[1][zheng]#有零就去零再加
                else:
                    self.Uppercase = self.Uppercase + self.ND[1][zheng]
            elif self.number[i]=='0':
                self.Uppercase = self.Uppercase[0:-1] + '零'
            else:
                self.Uppercase=self.Uppercase+self.ND[yu]#"拾佰仟"中选择
        if intlength==length:
            self.Uppercase=self.Uppercase+"整"
        else:
            for i in range(intlength+1,length):
                self.Uppercase=self.Uppercase+self.NU[eval(self.number[i])]
                self.Uppercase=self.Uppercase+self.ND[intlength-i]
        print(self.Uppercase)
        return self.Uppercase

def setWindow(parent_window):
    window = Toplevel(parent_window)
    # 给窗口的可视化取名
    window.title('NumToUpper')
    # 设定窗口大小(长*宽)
    window.geometry('360x400')

    # 计算器的显示屏
    # Entry为文本输入域
    # display = Entry(window,width=10, bd=4,font=("Arial", 24),justify='right',relief=SUNKEN)
    # label标签可以显示文字
    frame = tk.Frame(window, width=10, bd=4, relief=SUNKEN)
    frame.grid(row=0, column=0, columnspan=4, sticky='ew')
    display = tk.Label(frame, width=10, bd=4, font=("Arial", 18), justify='right', text='0', anchor='e')
    display0 = tk.Label(frame, width=10, bd=4, font=("Arial", 24), justify='right', anchor='e')
    # 方格放置
    display0.grid(row=0, column=0, columnspan=4, sticky='ew', ipady=20)
    display.grid(row=1, column=0, columnspan=4, sticky='ew', ipady=20)

    scientific_computing_buttons = [
            '7', '8', '9', 'C',
            '4', '5', '6', '',
            '1', '2', '3', '',
            '00', '0', '.', 'd'
    ]
    nTu = NumToUpper()

    def show():
        display0.config(text=nTu.displayNumber())
        display.config(text=nTu.displayUpper())

    def on_click(s):
        if re.match(r'^[0-9]$', s) or s == ".":
            if nTu.addNumber(s):
                show()
        elif s == "d":
            nTu.removeNumber()
            show()
        elif s == 'C':
            nTu.setNumber('0')
            show()

    s_row_value = 1
    s_col_value = 0
    for button in scientific_computing_buttons:
        button1 = Button(window, text=button, font=('Arial', 10), width=9, command=lambda s=button: on_click(s))
        button1.grid(row=s_row_value, column=s_col_value, sticky='nsew', padx=3, pady=3, ipadx=4, ipady=4)
        s_col_value += 1
        if s_col_value > 3:
            s_row_value += 1
            s_col_value = 0

    for i in range(5):
        window.columnconfigure(i, weight=1)
    for i in range(11):
        window.rowconfigure(i, weight=1)
    window.mainloop()


